package com.ityu.aop;

import com.ityu.mapper.EmpLoginLogMapper;
import com.ityu.mapper.OperateLogMapper;
import com.ityu.pojo.Emp;
import com.ityu.pojo.Log.EmpLoginLog;
import com.ityu.pojo.LoginInfo;
import com.ityu.pojo.result.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Aspect
@Component
public class LoginAspect {

    @Autowired
    EmpLoginLogMapper empLoginLogMapper;

    @Around("execution(* com.ityu.controller.LoginController.login(com.ityu.pojo.Emp) )")
    public Object RecordLoginInfo(ProceedingJoinPoint joinPoint) throws Throwable {
        Emp emp = (Emp)joinPoint.getArgs()[0];

        String username = emp.getUsername();
        String password = emp.getPassword();

        long s = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long e = System.currentTimeMillis();

        Long costTime = e-s;
        Result re = (Result)result;
        Short isSuccess = Short.parseShort(re.getCode().toString());
        String jwt = null;
        if (isSuccess == 1){
            LoginInfo loginInfo = (LoginInfo)re.getData();
             jwt = loginInfo.getToken();
        }
        EmpLoginLog log = new EmpLoginLog(null,username,password, LocalDateTime.now(),isSuccess,jwt,costTime);
        empLoginLogMapper.insert(log);
        return result;
    }


}
